/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * databaseSettings.java
 *
 * Created on Jul 12, 2011, 8:27:11 PM
 */

package cnsatmsmart.automation.mmi.view.dialogs;

import cnsatmsmart.CNSATMSMARTView;
import cnsatmsmart.automation.mmi.helper.utilities.GlobalEnvVars;
import cnsatmsmart.automation.mmi.model.database.DataBase;
import java.awt.BorderLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author SIK - PTIK, BPPT
 */
public class databaseSettings extends javax.swing.JDialog {

    /* konstanta */
        /**
         * id untuk tabel airports
         */
        public static final int AIRPORTS = 0;
        /**
         * id untuk tabel fixes
         */
        public static final int FIXES = 1;
        /**
         * id untuk tabel navaids
         */
        public static final int NAVAIDS = 2;
        /**
         * id untuk tabel airways
         */
        public static final int AIRWAYS = 3;
        /**
         * id untuk tabel flightplan
         */
        public static final int FLIGHT_PLAN = 4;
        /**
         * array dari tabel id
         */
        public static final int[] TABLES_ID = {AIRPORTS, FIXES, NAVAIDS, AIRWAYS, FLIGHT_PLAN};

    /* atribut-atribut yang dimiliki oleh kelas databaseSetting */
        private DefaultTableModel airportModel;
        private DefaultTableModel fixesModel;
        private DefaultTableModel navaidsModel;
        private DefaultTableModel airwaysModel;
        private DefaultTableModel flightplanModel;

        private JTable airportTable;
        private JTable fixesTable;
        private JTable navaidsTable;
        private JTable airwaysTable;
        private JTable flightplanTable;

        private addEditDatabaseDialog addEditWindow;

        private DataBase db;
        private static String db_Connection = "jdbc:mysql:///MMI?user=" + GlobalEnvVars.db_Username + "&password=" + GlobalEnvVars.db_Password;

        CNSATMSMARTView parent;


    /** contructor - default constructor <br />
     * membuat form databaseSetting yang baru
     * @param parent jendela yang memanggilnya
     * @param modal ada atau tidaknya dialog
     */
        public databaseSettings(java.awt.Frame parent, boolean modal) {
            super(parent, modal);
            initComponents();
        }

    /**
     * constructor - user defined constructor <br />
     * membuat form databaseSetting yang baru dengan telah terisi tabel-tabel yang ada pada database
     */
        public databaseSettings(CNSATMSMARTView parent)
        {
            this.parent = parent;
            initComponents();
            initComp();
            initAirportTable();
            initFixesTable();
            initNavaidsTable();
            initAirwaysTable();
            initFlightPlanTable();
        }

    /**
     * I.S. : sebarang <br>
     * F.S. : database berhasil tersambung <br>
     * proses : membuka koneksi jdbc:mysql dengan database yang telah didefinisikan sebelumnya
     */
    private void initComp()
    {
        Image frameIcon = GlobalEnvVars.kit.getImage(getClass().getResource("/cnsatmsmart/resources/"+GlobalEnvVars.icon));
        setIconImage(frameIcon);
        setTitle("Database Window");
        setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
        setAlwaysOnTop(true);
        setResizable(false);

        try {
            db = new DataBase(db_Connection);
        } catch (Exception e) {
            System.out.println("failed to connect to database");
        }
    }

    /**
     * me-retrieve tabel airport dari database, mengisikannya pada this.airportModel dan menampilkannya pada jendela dialog
     */
    private void initAirportTable()
    {
        String[] columnNames = {"ID Airport",
                                "Latitude Degree",
                                "Latitude Minute",
                                "Latitude Second",
                                "Latitude Sign",
                                "Longitude Degree",
                                "Longitude Minute",
                                "Longitude Second",
                                "Longitude Sign"};
        this.airportModel = new DefaultTableModel(columnNames, 0);

        this.airportTable = new JTable( this.airportModel )
        {
            public boolean isCellEditable(int rowIndex, int colIndex) {
                return false; //Disallow the editing of any cell
            }
        };

        this.airportTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        this.airportTable.getTableHeader().setReorderingAllowed( false );
        
        try {
            db.createQuery("SELECT * FROM airports");

            while (db.getResultSet().next()) {
                Object[] row = {db.getResultSet().getString("ID_Airports"),
                                db.getResultSet().getInt("Lat_Deg"),
                                db.getResultSet().getInt("Lat_Min"),
                                db.getResultSet().getFloat("Lat_Sec"),
                                db.getResultSet().getString("Lat_Sign"),
                                db.getResultSet().getInt("Lon_Deg"),
                                db.getResultSet().getInt("Lon_Min"),
                                db.getResultSet().getFloat("Lon_Sec"),
                                db.getResultSet().getString("Lon_Sign")};
                airportModel.addRow(row);
            }
        } catch (SQLException ex) {
            System.out.println("failed fetch from table airports");
        }

        JScrollPane scrollPane = new JScrollPane(this.airportTable);
        this.airportsPanel.setLayout(new BorderLayout());
        this.airportsPanel.add(scrollPane);

        JPanel p = new JPanel();
        p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));

        JButton editAirports = new JButton("Edit Airport");
        editAirports.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e)
                    {btnEditActionPerformed(TABLES_ID[AIRPORTS]);}
            }
        );

        JButton deleteAirports = new JButton("Delete Airport");
        deleteAirports.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent e)
                    {btnDeleteActionPerformed(TABLES_ID[AIRPORTS]);}
            }
        );

        JButton addAirports = new JButton("Add New Airport");
        addAirports.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent e)
                    {btnAddActionPerformed(TABLES_ID[AIRPORTS]);}
            }
        );
        
        p.add(Box.createHorizontalGlue());
        p.add(addAirports);
        p.add(editAirports);
        p.add(deleteAirports);
        
        this.airportsPanel.add(p, BorderLayout.SOUTH);
    }

    /**
     * me-retrieve tabel fixes dari database, mengisikannya pada this.fixesModel dan menampilkannya pada jendela dialog
     */
    private void initFixesTable()
    {
        String[] columnNames = {"ID Fix",
                                "Latitude Degree",
                                "Latitude Minute",
                                "Latitude Second",
                                "Latitude Sign",
                                "Longitude Degree",
                                "Longitude Minute",
                                "Longitude Second",
                                "Longitude Sign"};
        this.fixesModel = new DefaultTableModel(columnNames, 0);

        this.fixesTable = new JTable( this.fixesModel )
        {
            public boolean isCellEditable(int rowIndex, int colIndex) {
                return false; //Disallow the editing of any cell
            }
        };

        this.fixesTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        this.fixesTable.getTableHeader().setReorderingAllowed( false );

        try {
            db.createQuery("SELECT * FROM fixes");

            while (db.getResultSet().next()) {
                Object[] row = {db.getResultSet().getString("ID_Fixes"),
                                db.getResultSet().getInt("Lat_Deg"),
                                db.getResultSet().getInt("Lat_Min"),
                                db.getResultSet().getFloat("Lat_Sec"),
                                db.getResultSet().getString("Lat_Sign"),
                                db.getResultSet().getInt("Lon_Deg"),
                                db.getResultSet().getInt("Lon_Min"),
                                db.getResultSet().getFloat("Lon_Sec"),
                                db.getResultSet().getString("Lon_Sign")};
                fixesModel.addRow(row);
            }
        } catch (SQLException ex) {
            System.out.println("failed fetch from table fixes");
        }

        JScrollPane scrollPane = new JScrollPane(this.fixesTable);
        this.fixesPanel.setLayout(new BorderLayout());
        this.fixesPanel.add(scrollPane);

        JPanel p = new JPanel();
        p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));

        JButton editFixes = new JButton("Edit Fixes");
        editFixes.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e)
                    {btnEditActionPerformed(TABLES_ID[FIXES]);}
            }
        );

        JButton deleteFixes = new JButton("Delete Fixes");
        deleteFixes.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent e)
                    {btnDeleteActionPerformed(TABLES_ID[FIXES]);}
            }
        );

        JButton addFixes = new JButton("Add New Fixes");
        addFixes.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent e)
                    {btnAddActionPerformed(TABLES_ID[FIXES]);}
            }
        );

        p.add(Box.createHorizontalGlue());
        p.add(addFixes);
        p.add(editFixes);
        p.add(deleteFixes);

        this.fixesPanel.add(p, BorderLayout.SOUTH);
    }

    /**
     * me-retrieve tabel navaids dari database, mengisikannya pada this.navaidsModel dan menampilkannya pada jendela dialog
     */
    private void initNavaidsTable()
    {
        String[] columnNames = {"ID Fix",
                                "Latitude Degree",
                                "Latitude Minute",
                                "Latitude Second",
                                "Latitude Sign",
                                "Longitude Degree",
                                "Longitude Minute",
                                "Longitude Second",
                                "Longitude Sign"};
        this.navaidsModel = new DefaultTableModel(columnNames, 0);

        this.navaidsTable = new JTable( this.navaidsModel )
        {
            public boolean isCellEditable(int rowIndex, int colIndex) {
                return false; //Disallow the editing of any cell
            }
        };

        this.navaidsTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        this.navaidsTable.getTableHeader().setReorderingAllowed( false );

        try {
            db.createQuery("SELECT * FROM navaids");

            while (db.getResultSet().next()) {
                Object[] row = {db.getResultSet().getString("ID_Navaids"),
                                db.getResultSet().getInt("Lat_Deg"),
                                db.getResultSet().getInt("Lat_Min"),
                                db.getResultSet().getFloat("Lat_Sec"),
                                db.getResultSet().getString("Lat_Sign"),
                                db.getResultSet().getInt("Lon_Deg"),
                                db.getResultSet().getInt("Lon_Min"),
                                db.getResultSet().getFloat("Lon_Sec"),
                                db.getResultSet().getString("Lon_Sign")};
                navaidsModel.addRow(row);
            }
        } catch (SQLException ex) {
            System.out.println("failed fetch from table navaids");
        }

        JScrollPane scrollPane = new JScrollPane(this.navaidsTable);
        this.navaidsPanel.setLayout(new BorderLayout());
        this.navaidsPanel.add(scrollPane);

        JPanel p = new JPanel();
        p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));

        JButton editNavaids = new JButton("Edit Navaids");
        editNavaids.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e)
                    {btnEditActionPerformed(TABLES_ID[NAVAIDS]);}
            }
        );

        JButton deleteNavaids = new JButton("Delete Navaids");
        deleteNavaids.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent e)
                    {btnDeleteActionPerformed(TABLES_ID[NAVAIDS]);}
            }
        );

        JButton addNavaids = new JButton("Add New Navaids");
        addNavaids.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent e)
                    {btnAddActionPerformed(TABLES_ID[NAVAIDS]);}
            }
        );

        p.add(Box.createHorizontalGlue());
        p.add(addNavaids);
        p.add(editNavaids);
        p.add(deleteNavaids);

        this.navaidsPanel.add(p, BorderLayout.SOUTH);
    }

    /**
     * me-retrieve tabel airways dari database, mengisikannya pada this.airwaysModel dan menampilkannya pada jendela dialog
     */
    private void initAirwaysTable()
    {
        String[] columnNames = {"ID Airway",
                                "Path Airway"};
        this.airwaysModel = new DefaultTableModel(columnNames, 0);

        this.airwaysTable = new JTable( this.airwaysModel )
        {
            public boolean isCellEditable(int rowIndex, int colIndex) {
                return false; //Disallow the editing of any cell
            }
        };

        this.airwaysTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        this.airwaysTable.getTableHeader().setReorderingAllowed( false );
        this.airwaysTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        this.airwaysTable.getColumnModel().getColumn(0).setPreferredWidth(100);
        this.airwaysTable.getColumnModel().getColumn(1).setPreferredWidth(580);
        
        try {
            db.createQuery("SELECT * FROM airways");

            while (db.getResultSet().next()) {
                Object[] row = {db.getResultSet().getString("ID_Airways"),
                                db.getResultSet().getString("Path_Airways")};
                airwaysModel.addRow(row);
            }
        } catch (SQLException ex) {
            System.out.println("failed fetch from table airways");
        }

        JScrollPane scrollPane = new JScrollPane(this.airwaysTable);
        this.airwaysPanel.setLayout(new BorderLayout());
        this.airwaysPanel.add(scrollPane);

        JPanel p = new JPanel();
        p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));

        JButton editAirways = new JButton("Edit Airways");
        editAirways.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e)
                    {btnEditActionPerformed(TABLES_ID[AIRWAYS]);}
            }
        );

        JButton deleteAirways = new JButton("Delete Airways");
        deleteAirways.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent e)
                    {btnDeleteActionPerformed(TABLES_ID[AIRWAYS]);}
            }
        );

        JButton addAirways = new JButton("Add New Airways");
        addAirways.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent e)
                    {btnAddActionPerformed(TABLES_ID[AIRWAYS]);}
            }
        );

        p.add(Box.createHorizontalGlue());
        p.add(addAirways);
        p.add(editAirways);
        p.add(deleteAirways);

        this.airwaysPanel.add(p, BorderLayout.SOUTH);
    }

    /**
     * me-retrieve tabel flightplan dari database, mengisikannya pada this.flightplanModel dan menampilkannya pada jendela dialog
     */
    private void initFlightPlanTable()
    {
        String[] columnNames = {"ID Flight",
                                "Path Flight Plan"};
        this.flightplanModel = new DefaultTableModel(columnNames, 0);

        this.flightplanTable = new JTable( this.flightplanModel )
        {
            public boolean isCellEditable(int rowIndex, int colIndex) {
                return false; //Disallow the editing of any cell
            }
        };

        this.flightplanTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        this.flightplanTable.getTableHeader().setReorderingAllowed( false );
        this.airwaysTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        this.flightplanTable.getColumnModel().getColumn(0).setPreferredWidth(100);
        this.flightplanTable.getColumnModel().getColumn(1).setPreferredWidth(580);

        try {
            db.createQuery("SELECT * FROM flightplan");

            while (db.getResultSet().next()) {
                Object[] row = {db.getResultSet().getString("ID_Flight"),
                                db.getResultSet().getString("Path_FlightPlan")};
                flightplanModel.addRow(row);
            }
        } catch (SQLException ex) {
            System.out.println("failed fetch from table flight plan");
        }

        JScrollPane scrollPane = new JScrollPane(this.flightplanTable);
        this.flightplanPanel.setLayout(new BorderLayout());
        this.flightplanPanel.add(scrollPane);

        JPanel p = new JPanel();
        p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));

        JButton editFlightplan = new JButton("Edit Flight Plan");
        editFlightplan.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e)
                    {btnEditActionPerformed(TABLES_ID[FLIGHT_PLAN]);}
            }
        );

        JButton deleteFlightplan = new JButton("Delete Flight Plan");
        deleteFlightplan.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent e)
                    {btnDeleteActionPerformed(TABLES_ID[FLIGHT_PLAN]);}
            }
        );

        JButton addFlightplan = new JButton("Add New Flight Plan");
        addFlightplan.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent e)
                    {btnAddActionPerformed(TABLES_ID[FLIGHT_PLAN]);}
            }
        );

        p.add(Box.createHorizontalGlue());
        p.add(addFlightplan);
        p.add(editFlightplan);
        p.add(deleteFlightplan);

        this.flightplanPanel.add(p, BorderLayout.SOUTH);
    }

    /**
     * mengupdate suatu row jika diedit atau menambahkan row baru jika diadd
     * @param table_id ID tabel yang diupdate/diperbaharui
     * @param isEdit apakah tabel diedit atau ditambahkan
     * @param isi isi dari tabel yang diperbaharui
     */
    public void updateTable(int table_id, boolean isEdit, String[] isi)
    {
        switch (table_id)
        {
            case AIRPORTS:
            {
                if (isEdit)
                {
                    int row = airportTable.getSelectedRow();
                    airportModel.setValueAt(isi[1], row, 1);
                    airportModel.setValueAt(isi[2], row, 2);
                    airportModel.setValueAt(isi[3], row, 3);
                    airportModel.setValueAt(isi[4], row, 4);
                    airportModel.setValueAt(isi[5], row, 5);
                    airportModel.setValueAt(isi[6], row, 6);
                    airportModel.setValueAt(isi[7], row, 7);
                    airportModel.setValueAt(isi[8], row, 8);
                }
                else
                {
                    airportModel.addRow(isi);
                }
                break;
            }
            case FIXES:
            {
                if (isEdit)
                {
                    int row = fixesTable.getSelectedRow();
                    fixesModel.setValueAt(isi[1], row, 1);
                    fixesModel.setValueAt(isi[2], row, 2);
                    fixesModel.setValueAt(isi[3], row, 3);
                    fixesModel.setValueAt(isi[4], row, 4);
                    fixesModel.setValueAt(isi[5], row, 5);
                    fixesModel.setValueAt(isi[6], row, 6);
                    fixesModel.setValueAt(isi[7], row, 7);
                    fixesModel.setValueAt(isi[8], row, 8);
                }
                else
                {
                    fixesModel.addRow(isi);
                }
                break;
            }
            case NAVAIDS:
            {
                if (isEdit)
                {
                    int row = navaidsTable.getSelectedRow();
                    navaidsModel.setValueAt(isi[1], row, 1);
                    navaidsModel.setValueAt(isi[2], row, 2);
                    navaidsModel.setValueAt(isi[3], row, 3);
                    navaidsModel.setValueAt(isi[4], row, 4);
                    navaidsModel.setValueAt(isi[5], row, 5);
                    navaidsModel.setValueAt(isi[6], row, 6);
                    navaidsModel.setValueAt(isi[7], row, 7);
                    navaidsModel.setValueAt(isi[8], row, 8);
                }
                else
                {
                    navaidsModel.addRow(isi);
                }
                break;
            }
            case AIRWAYS:
            {
                if (isEdit)
                {
                    int row = airwaysTable.getSelectedRow();
                    airwaysModel.setValueAt(isi[1], row, 1);
                }
                else
                {
                    airwaysModel.addRow(isi);
                }
                break;
            }
            case FLIGHT_PLAN:
            {
                if (isEdit)
                {
                    int row = flightplanTable.getSelectedRow();
                    flightplanModel.setValueAt(isi[1], row, 1);
                }
                else
                {
                    flightplanModel.addRow(isi);
                }
                break;
            }
        }
    }

    /**
     * menghapus airport, fixes, atau navaids yang digunakan pada airways dan flightplan bila airport, fixes, atau navaids tersebut dihapus
     * @param id ID dari airport, fixes, atau navaids
     */
    private void removeFromAirwaysAndFlightplan(String id)
    {
        for (int i=0; i<airwaysModel.getRowCount(); ++i)
        {
            String str = (String)airwaysModel.getValueAt(i, 1);
            String temp = str.replaceAll(";" + id, "");
            String temp2 = temp.replace(id + ";", "");
            airwaysModel.setValueAt(temp2,i, 1);
            db.createUpdateQuery("UPDATE airways SET Path_Airways = '" + temp2 + "' WHERE ID_Airways = '" + (String)airwaysModel.getValueAt(i, 0) + "';");
            
        }
        
        for (int i=0; i<flightplanModel.getRowCount(); ++i)
        {
            String str = (String)flightplanModel.getValueAt(i, 1);
            String temp = str.replaceAll(";" + id, "");
            String temp2 = temp.replace(id + ";", "");
            flightplanModel.setValueAt(temp2,i, 1);
            db.createUpdateQuery("UPDATE flightplan SET Path_flightplan = '" + temp2 + "' WHERE ID_flight = '" + (String)flightplanModel.getValueAt(i, 0) + "';");
            
        }
    }
        

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        airportsPanel = new javax.swing.JPanel();
        fixesPanel = new javax.swing.JPanel();
        navaidsPanel = new javax.swing.JPanel();
        airwaysPanel = new javax.swing.JPanel();
        flightplanPanel = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N

        jTabbedPane1.setName("jTabbedPane1"); // NOI18N

        airportsPanel.setName("airportsPanel"); // NOI18N

        javax.swing.GroupLayout airportsPanelLayout = new javax.swing.GroupLayout(airportsPanel);
        airportsPanel.setLayout(airportsPanelLayout);
        airportsPanelLayout.setHorizontalGroup(
            airportsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 672, Short.MAX_VALUE)
        );
        airportsPanelLayout.setVerticalGroup(
            airportsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 378, Short.MAX_VALUE)
        );

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(cnsatmsmart.CNSATMSMARTApp.class).getContext().getResourceMap(databaseSettings.class);
        jTabbedPane1.addTab(resourceMap.getString("airportsPanel.TabConstraints.tabTitle"), airportsPanel); // NOI18N

        fixesPanel.setName("fixesPanel"); // NOI18N

        javax.swing.GroupLayout fixesPanelLayout = new javax.swing.GroupLayout(fixesPanel);
        fixesPanel.setLayout(fixesPanelLayout);
        fixesPanelLayout.setHorizontalGroup(
            fixesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 672, Short.MAX_VALUE)
        );
        fixesPanelLayout.setVerticalGroup(
            fixesPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 378, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab(resourceMap.getString("fixesPanel.TabConstraints.tabTitle"), fixesPanel); // NOI18N

        navaidsPanel.setName("navaidsPanel"); // NOI18N

        javax.swing.GroupLayout navaidsPanelLayout = new javax.swing.GroupLayout(navaidsPanel);
        navaidsPanel.setLayout(navaidsPanelLayout);
        navaidsPanelLayout.setHorizontalGroup(
            navaidsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 672, Short.MAX_VALUE)
        );
        navaidsPanelLayout.setVerticalGroup(
            navaidsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 378, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab(resourceMap.getString("navaidsPanel.TabConstraints.tabTitle"), navaidsPanel); // NOI18N

        airwaysPanel.setName("airwaysPanel"); // NOI18N

        javax.swing.GroupLayout airwaysPanelLayout = new javax.swing.GroupLayout(airwaysPanel);
        airwaysPanel.setLayout(airwaysPanelLayout);
        airwaysPanelLayout.setHorizontalGroup(
            airwaysPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 672, Short.MAX_VALUE)
        );
        airwaysPanelLayout.setVerticalGroup(
            airwaysPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 378, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab(resourceMap.getString("airwaysPanel.TabConstraints.tabTitle"), airwaysPanel); // NOI18N

        flightplanPanel.setName("flightplanPanel"); // NOI18N

        javax.swing.GroupLayout flightplanPanelLayout = new javax.swing.GroupLayout(flightplanPanel);
        flightplanPanel.setLayout(flightplanPanelLayout);
        flightplanPanelLayout.setHorizontalGroup(
            flightplanPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 672, Short.MAX_VALUE)
        );
        flightplanPanelLayout.setVerticalGroup(
            flightplanPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 378, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab(resourceMap.getString("flightplanPanel.TabConstraints.tabTitle"), flightplanPanel); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 677, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 406, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    //<editor-fold defaultstate="collapsed" desc="edit-delete-add handler">
        private void btnEditActionPerformed(int table_id)
        {
            switch (table_id)
            {
                case AIRPORTS:
                {
                    if (airportTable.getSelectedRow() != -1)
                    {
                        String ID = (String)airportModel.getValueAt(airportTable.getSelectedRow(), 0);
                        addEditWindow = new addEditDatabaseDialog(new JFrame(), true, this, db, TABLES_ID[AIRPORTS], ID, true, true);
                        addEditWindow.setVisible(true);
                    }
                    else
                    {
                        JOptionPane.showMessageDialog(this, "No selected row");
                    }
                    break;
                }
                case FIXES:
                {
                    if (fixesTable.getSelectedRow() != -1)
                    {
                        String ID = (String)fixesModel.getValueAt(fixesTable.getSelectedRow(), 0);
                        addEditWindow = new addEditDatabaseDialog(new JFrame(), true, this, db, TABLES_ID[FIXES], ID, true, true);
                        addEditWindow.setVisible(true);
                    }
                    else
                    {
                        JOptionPane.showMessageDialog(this, "No selected row");
                    }
                    break;
                }
                case NAVAIDS:
                {
                    if (navaidsTable.getSelectedRow() != -1)
                    {
                        String ID = (String)navaidsModel.getValueAt(navaidsTable.getSelectedRow(), 0);
                        addEditWindow = new addEditDatabaseDialog(new JFrame(), true, this, db, TABLES_ID[NAVAIDS], ID, true, true);
                        addEditWindow.setVisible(true);
                    }
                    else
                    {
                        JOptionPane.showMessageDialog(this, "No selected row");
                    }
                    break;
                }
                case AIRWAYS:
                {
                    if (airwaysTable.getSelectedRow() != -1)
                    {
                        String ID = (String)airwaysModel.getValueAt(airwaysTable.getSelectedRow(), 0);
                        addEditWindow = new addEditDatabaseDialog(new JFrame(), true, this, db, TABLES_ID[AIRWAYS], ID, false, true);
                        addEditWindow.setVisible(true);
                    }
                    else
                    {
                        JOptionPane.showMessageDialog(this, "No selected row");
                    }
                    break;
                }
                case FLIGHT_PLAN:
                {
                    if (flightplanTable.getSelectedRow() != -1)
                    {
                        String ID = (String)flightplanModel.getValueAt(flightplanTable.getSelectedRow(), 0);
                        addEditWindow = new addEditDatabaseDialog(new JFrame(), true, this, db, TABLES_ID[FLIGHT_PLAN], ID, false, true);
                        addEditWindow.setVisible(true);
                    }
                    else
                    {
                        JOptionPane.showMessageDialog(this, "No selected row");
                    }
                    break;
                }
                default:
                    break;
            }
            this.parent.updateLayer(table_id);
        }

        private void btnDeleteActionPerformed(int tabel_id)
        {
            switch (tabel_id)
            {
                case AIRPORTS:
                {
                    if (airportTable.getSelectedRow() != -1)
                    {
                        int n = JOptionPane.showConfirmDialog(this,"Are you sure want to delete?","Warning",JOptionPane.YES_NO_OPTION);
                        if (n == JOptionPane.YES_OPTION)
                        {
                            String ID = (String)airportModel.getValueAt(airportTable.getSelectedRow(), 0);
                            db.createUpdateQuery("DELETE FROM airports WHERE ID_Airports = '" + ID + "';");
                            airportModel.removeRow(airportTable.getSelectedRow());

                            int t = JOptionPane.showConfirmDialog(this,"Are you want to delete this airport from airways and flightplan?","Warning",JOptionPane.YES_NO_OPTION);
                            if (t == JOptionPane.YES_OPTION)
                            {
                                removeFromAirwaysAndFlightplan(ID);
                            }

                        }
                    }
                    else
                        JOptionPane.showMessageDialog(this, "No selected row");
                    break;
                }
                case FIXES:
                {
                    if (fixesTable.getSelectedRow() != -1)
                    {
                        int n = JOptionPane.showConfirmDialog(this,"Are you sure want to delete?","Warning",JOptionPane.YES_NO_OPTION);
                        if (n == JOptionPane.YES_OPTION)
                        {
                            String ID = (String)fixesModel.getValueAt(fixesTable.getSelectedRow(), 0);
                            db.createUpdateQuery("DELETE FROM fixes WHERE ID_Fixes = '" + ID + "';");
                            fixesModel.removeRow(fixesTable.getSelectedRow());

                            int t = JOptionPane.showConfirmDialog(this,"Are you want to delete this fixes from airways and flightplan?","Warning",JOptionPane.YES_NO_OPTION);
                            if (t == JOptionPane.YES_OPTION)
                            {
                                removeFromAirwaysAndFlightplan(ID);
                            }
                        }
                    }
                    else
                        JOptionPane.showMessageDialog(this, "No selected row");
                    break;
                }
                case NAVAIDS:
                {
                    if (navaidsTable.getSelectedRow() != -1)
                    {
                        int n = JOptionPane.showConfirmDialog(this,"Are you sure want to delete?","Warning",JOptionPane.YES_NO_OPTION);
                        if (n == JOptionPane.YES_OPTION)
                        {
                            String ID = (String)navaidsModel.getValueAt(navaidsTable.getSelectedRow(), 0);
                            db.createUpdateQuery("DELETE FROM navaids WHERE ID_Navaids= '" + ID + "';");
                            navaidsModel.removeRow(navaidsTable.getSelectedRow());

                            int t = JOptionPane.showConfirmDialog(this,"Are you want to delete this navaids from airways and flightplan?","Warning",JOptionPane.YES_NO_OPTION);
                            if (t == JOptionPane.YES_OPTION)
                            {
                                removeFromAirwaysAndFlightplan(ID);
                            }
                        }
                    }
                    else
                        JOptionPane.showMessageDialog(this, "No selected row");
                    break;
                }
                case AIRWAYS:
                {
                    if (airwaysTable.getSelectedRow() != -1)
                    {
                        int n = JOptionPane.showConfirmDialog(this,"Are you sure want to delete?","Warning",JOptionPane.YES_NO_OPTION);
                        if (n == JOptionPane.YES_OPTION)
                        {
                            String ID = (String)airwaysModel.getValueAt(airwaysTable.getSelectedRow(), 0);
                            db.createUpdateQuery("DELETE FROM airways WHERE ID_Airways = '" + ID + "';");
                            airwaysModel.removeRow(airwaysTable.getSelectedRow());
                        }
                    }
                    else
                        JOptionPane.showMessageDialog(this, "No selected row");
                    break;
                }
                case FLIGHT_PLAN:
                {
                    if (flightplanTable.getSelectedRow() != -1)
                    {
                        int n = JOptionPane.showConfirmDialog(this,"Are you sure want to delete?","Warning",JOptionPane.YES_NO_OPTION);
                        if (n == JOptionPane.YES_OPTION)
                        {
                            String ID = (String)flightplanModel.getValueAt(flightplanTable.getSelectedRow(), 0);
                            db.createUpdateQuery("DELETE FROM flightplan WHERE ID_Flight = '" + ID + "';");
                            flightplanModel.removeRow(flightplanTable.getSelectedRow());
                        }
                    }
                    else
                        JOptionPane.showMessageDialog(this, "No selected row");
                    break;
                }
                default:
                {
                    break;
                }
            }
            this.parent.updateLayer(tabel_id);
        }

        private void btnAddActionPerformed(int table_id)
        {
            JOptionPane.showMessageDialog(this, "Character ID must be UPPERCASE");
            switch (table_id)
            {
                case AIRPORTS:
                {
                    addEditWindow = new addEditDatabaseDialog(new JFrame(), true, this, db, TABLES_ID[AIRPORTS], null, true, false);
                    addEditWindow.setVisible(true);
                    break;
                }
                case FIXES:
                {
                    addEditWindow = new addEditDatabaseDialog(new JFrame(), true, this, db, TABLES_ID[FIXES], null, true, false);
                    addEditWindow.setVisible(true);
                    break;
                }
                case NAVAIDS:
                {
                    addEditWindow = new addEditDatabaseDialog(new JFrame(), true, this, db, TABLES_ID[NAVAIDS], null, true, false);
                    addEditWindow.setVisible(true);
                    break;
                }
                case AIRWAYS:
                {
                    addEditWindow = new addEditDatabaseDialog(new JFrame(), true, this, db, TABLES_ID[AIRWAYS], null, false, false);
                    addEditWindow.setVisible(true);
                    break;
                }
                case FLIGHT_PLAN:
                {
                    addEditWindow = new addEditDatabaseDialog(new JFrame(), true, this, db, TABLES_ID[FLIGHT_PLAN], null, false, false);
                    addEditWindow.setVisible(true);
                    break;
                }
                default:
                    break;
            }
            this.parent.updateLayer(table_id);
        }
    //</editor-fold>
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel airportsPanel;
    private javax.swing.JPanel airwaysPanel;
    private javax.swing.JPanel fixesPanel;
    private javax.swing.JPanel flightplanPanel;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JPanel navaidsPanel;
    // End of variables declaration//GEN-END:variables

}
